package com.zone.vehicle.superApps.view.common.base;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.zone.vehicle.superApps.view.common.HandledFragment;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseActivity<T extends HandledFragment> extends AppCompatActivity {

    private Map<String, T> pages = new LinkedHashMap<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFragmentPages();
        setContentView();
    }

    protected abstract void initFragmentPages();

    protected abstract void setContentView();

//    @Override
//    public void onPageChanged(String pageName) {
//        HandledFragment fragment = pages.get(pageName);
//        if (fragment != null && fragment.getClass().getSimpleName().equals(pageName)){
//            getSupportFragmentManager().beginTransaction()
//                    .replace(R.id.container, fragment)
//                    .addToBackStack(null)
//                    .commitNow();
//        }
//    }

    public Map<String, T> getPagesMap() {
        return pages;
    }

    public List<T> getPages() {
        return new ArrayList<>(pages.values());
    }


    public T getPage(int position){
        List<T> list = new ArrayList<>(pages.values());
        return list.get(position);
    }

    public T getPage(String pageName){
        return pages.get(pageName);
    }

    public int getPagePosition(String pageName){
        int position = 0;
        for(String key: pages.keySet()){
            if (key.equals(pageName)){
                return position;
            }
            position ++;
        }
        return position;
    }

    public void addPage(T fragment) {
        pages.put(fragment.getClass().getSimpleName(), fragment);
    }

}
